﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;

namespace WebFabricator.org
{
    public class Fabricator<T>:IWebFabricator<T>
    {
        public Fabricator()
        {
           
  
        }

        public void Fabricate(T context)
        {
            IWebFabric<T> fabric = new WebFabric<T>(context);
            var result = fabric.GetValue(typeof(IWebFabricatorFactory<>).GetNamespace().ToString());
            IWebFabricatorFactory<T> factory = result == null || String.IsNullOrEmpty(result.AttemptedValue) ?
                                                    (IWebFabricatorFactory<T>)new WebFabricatorFactory<T>() //.Wrap()
                                                  : ((IWebFabricatorFactory<T>)Activator.CreateInstance(Type.GetType(result.AttemptedValue)));//.Wrap();

            IWebFabricator<IWebFabric<T>> fabricator = factory.CreateFabricator(fabric);
            try
            {
                fabricator.Fabricate(fabric);
            }
            catch
            {
                throw;
            }
            finally
            {
                factory.ReleaseFabricator(fabricator);
            }
        }

        public void Dispose()
        {
            
        }
    }
}
